<template>
  <view class="content">
    <head-box></head-box>
    <view class="quntuanListPage-main-header">
      <div class="quntuanListPage-main-title">名师信息</div>
    </view>
    <view class="teacher_box">
      <view class="teacher_img">
        <image class="_img" :src="teacher.detail.icon" mode="scaleToFill"></image>
        <view class="teacher_title"> 名师信息：{{ teacher.detail.name || '' }} </view>
        <view class="teacher_con">
          <rich-text class="mian_body" :nodes="teacher.detail.content"></rich-text>
        </view>
      </view>
    </view>

    <view class="quntuanListPage-main-header">
      <div class="quntuanListPage-main-title">预约信息</div>
    </view>
    <view class="personalInfor-page-main">
      <view class="personalInfor-page-main-box">
        <view class="_main-item">
          <view class="_main-item-chunk _main-item-label"
            ><text>预约日期</text><text class="xing">*</text></view
          >
          <view class="_main-item-chunk _main-item-val">
            <view class="uni-input" @click="teacher.show = true">{{
              teacher.appointmentDate
            }}</view>
          </view>
        </view>
        <view class="_main-item">
          <view class="_main-item-chunk _main-item-label"
            ><text>上课地址</text><text class="xing">*</text></view
          >
          <view class="_main-item-chunk _main-item-val">
            <input
              cursorSpacing="10"
              class="_main-item-val-input"
              maxlength="50"
              v-model="teacher.classAddress"
              type="text"
              placeholder="请输入上课的地址"
            />
          </view>
        </view>
        <view class="_main-item">
          <view class="_main-item-chunk _main-item-label"
            ><text>服务时长</text><text class="xing">*</text></view
          >
          <view class="_main-item-chunk _main-item-val">
            <input
              cursorSpacing="10"
              class="_main-item-val-input"
              maxlength="5"
              v-model="teacher.serviceTime"
              type="number"
              placeholder="请输入需要的服务时长"
            />时
          </view>
        </view>
        <view class="_main-item">
          <view class="_main-item-chunk _main-item-label"
            ><text>联系人</text><text class="xing">*</text></view
          >
          <view class="_main-item-chunk _main-item-val">
            <input
              cursorSpacing="10"
              class="_main-item-val-input"
              maxlength="5"
              v-model="teacher.contactPerson"
              type="text"
              placeholder="联系人"
            />
          </view>
        </view>
        <view class="_main-item">
          <view class="_main-item-chunk _main-item-label"
            ><text>联系电话</text><text class="xing">*</text></view
          >
          <view class="_main-item-chunk _main-item-val">
            <input
              cursorSpacing="10"
              class="_main-item-val-input"
              maxlength="11"
              type="number"
              v-model="teacher.contactNumber"
              placeholder="联系电话"
            />
          </view>
        </view>
      </view>
    </view>
    <view class="btn tab-botton">
      <button type="primary" @click="onReservation">确定预约</button>
    </view>
    <tabs-box></tabs-box>
    <u-picker
      mode="time"
      v-model="teacher.show"
      :start-year="teacher.startTime"
      :end-year="teacher.endTime"
      :default-time="teacher.appointmentDate"
      :params="teacher.timeParams"
      @confirm="onDataChange"
    ></u-picker>
  </view>
</template>

<script>
  import { isPhoneReg } from '@/common/type'
  import { goLogin } from '@/common/base'
  import { getRunResourcesBase } from '@/api/training'
  import { addRunResourcesApply } from '@/api/resources'
  export default {
    data() {
      return {

        teacher: {
          timeParams:{
            year: true,
            month: true,
            day: true,
            hour: true,
            minute: true,
            second: false,
            // 选择时间的时间戳
            timestamp: true,
          },//时间方式
          show: false,
          teacherName: '', //老师或者视频 名称
          appointmentDate: '', //预约日期
          classAddress: '', //上课地址
          serviceTime: '', //服务时长
          contactPerson: '', //联系人
          contactNumber: '', //联系电话
          id: '', //老师或者视频的id
          endTime: '', //结束时间
          startTime: '', //开始时间
          detail: {}, //详情数据
        },
      }
    },
    onLoad(option) {
      const { id } = option
      this.getDate()
      this.teacher.id = id
      getRunResourcesBase({ id })
        .then((res) => {
          const { code, result } = res
          if (code === 200) {
            this.teacher.detail = result
          } else {
          }
        })
        .catch((e) => {
          const { message } = e
          this.$api.msg(message)
        })
    },
    computed: {},
    onShow(){
      // #ifdef H5
      console.log('分享')
      this.$wxSetShare()
      // #endif
    },
    methods: {
      //选择时间
      onDataChange(e) {
        const moment = this.$moment()
        const { day, month, year,hour,minute } = e
        const time = `${year}-${month}-${day} ${hour}:${minute}`
        if (time < moment.format('YYYY-MM-DD')) {
          this.$api.msg(`选择的时间已是过期,请重新选择`)
          return false
        }
        this.teacher.appointmentDate = time
      },
      //初始化时间
      getDate() {
        const time = this.$moment()
        const y = time.format('YYYY')
        const m = time.format('MM')
        const d = time.format('DD')
        const M = time.format('HH')
        const H = time.format('mm')
        const startTime = time.subtract(0, 'y').format('YYYY')
        const endTime = time.add(15, 'y').format('YYYY')
        this.teacher.appointmentDate = `${y}-${m}-${d} ${M}:${H}`
        this.teacher.startTime = startTime
        this.teacher.endTime = endTime
      },
      //预约
      onReservation() {
        const {
          appointmentDate,
          classAddress,
          serviceTime,
          contactPerson,
          contactNumber,
          detail,
          id,
        } = this.teacher
        if (!classAddress.length || !classAddress.trim()) {
          this.$api.msg('请输入上课的地址')
          return
        }
        if (!serviceTime.length || !serviceTime.trim()) {
          this.$api.msg('请输入需要的服务时长')
          return
        }
        if (+serviceTime>99.9) {
          this.$api.msg('服务时长最大为99.9个小时')
          return
        }
        if (!contactPerson.length || !contactPerson.trim()) {
          this.$api.msg('请输入联系人')
          return
        }
        if (!contactNumber.length || !contactNumber.trim()) {
          this.$api.msg('请输入联系电话')
          return
        }
        if (!isPhoneReg(contactNumber)) {
          this.$api.msg('联系电话不合法')
          return
        }

        const data = {
          startUseTime: this.$moment(appointmentDate).valueOf(), //预约时间
          address: classAddress, //地址 师资资源- 上课地址
          serviceDuration: serviceTime, //服务时长
          applyName: contactPerson, //联系人
          applyTel: contactNumber, //联系人方式
          resourcesId: id, //资源id
          ortherUserId: this.$store.state.userInfo.id, //申请人id
          note: '', //备注
          name: detail.name, //资源名称
          type:4,
          createTime: Date.parse(new Date()),
        }
        addRunResourcesApply(data)
          .then((res) => {
            const { code } = res
            if (code === 200) {
              this.$api.msg('预约成功')
              setTimeout(() => {
                uni.navigateTo({
                  url: '/pages/myInviteTeachers/myInviteTeachers',
                })
              }, 1000)
            } else {
              this.$api.msg('预约失败')
            }
          })
          .catch((e) => {
            uni.removeStorageSync('userInfo')
            uni.removeStorageSync('token')
            goLogin()
          })
      },
    },
  }
</script>

<style lang="scss">
  .content {
    height: 100vh;
  }
  .quntuanListPage-main-header {
    margin-left: 60rpx;
    margin-top: 40rpx;
    .quntuanListPage-main-title {
      font-size: 32rpx;
      color: #333;
      font-weight: 600;
      border-left: 6rpx solid #0071fc;
      padding-left: 20rpx;
    }
  }
  .teacher_box {
    margin: 30rpx 60rpx 0 60rpx;
    display: flex;
    flex-direction: row;
    .teacher_img {
      ._img {
        width: 183rpx;
        height: 236rpx;
        float: left;
        border-radius: 10rpx;
        margin-right: 30rpx;
      }
      .teacher_title {
        font-weight: bold;
        padding: 0rpx;
        margin-bottom: 10rpx;
      }
      .teacher_con {
        font-size: 26rpx;
        text-indent: 2em;
        line-height: 48rpx;
        color: #555555;
      }
    }
  }
  .personalInfor-page-main {
    margin-top: 10rpx;
    width: 100%;
    padding: 0 60rpx;
    box-sizing: border-box;
    .personalInfor-page-main-box {
      width: 100%;
      margin-bottom: 40rpx;
      ._main-item {
        // height: 86rpx;
        border-bottom: 1px solid rgba(200, 199, 204, 0.5);
        display: flex;
        flex-wrap: nowrap;
        justify-content: space-between;
        align-items: center;
        font-size: 30rpx;
        ._main-item-chunk {
          color: #000;
          line-height: 84rpx;
        }
        ._main-item-label {
          width: 30%;
        }
        ._main-item-val {
          width: 70%;
          display: flex;
          flex-wrap: nowrap;
          justify-content: flex-end;
          align-items: center;
          button {
            background-color: #dd524d;
            color: #ffffff;
          }
          ._main-item-val-head {
            width: 48rpx;
            height: 48rpx;
            border-radius: 50%;
            display: flex;
            .head-img {
              width: 100%;
              height: 100%;
              border-radius: 50%;
            }
          }
          ._main-item-val-input {
            width: 100%;
            text-align: right;
            display: flex;
            margin-right: 10rpx;
            ._main-item-val-input-text {
              display: flex;
              flex-wrap: nowrap;
              align-items: center;
            }
            .drop-dowm-img {
              width: 30rpx;
              height: 30rpx;
              margin-left: 10rpx;
              transform: rotateZ(270deg);
            }
          }
          .drop-dowm-img {
            width: 30rpx;
            height: 30rpx;
            margin-left: 10rpx;
            transform: rotateZ(270deg);
          }
        }
      }
    }
  }

  .xing {
    color: #ff0000;
    margin-left: 10rpx;
  }

  .btn {
    text-align: center;
    padding: 0 50rpx;
  }
</style>
